package com.qzh.TurnSimulator;

import com.qzh.TurnSimulator.entity.City;
import com.qzh.TurnSimulator.entity.GameTurn;
import com.qzh.TurnSimulator.entity.State;
import com.qzh.baseStruct.UnlimitQueue;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class GameManager {
    /**
     * 游戏回合队列
     * 无限队列
     */
    private UnlimitQueue<GameTurn> turns;
    private static GameManager gameManager;
    private List<State> states =new ArrayList<>();
    /**
     * 当前年
     */
    private int year;
    /**
     *当前月
     */
    private int month;
    /**
     * 步进速度，月为单位
     */
    private int step;






    /**
     * 输入年月步进并生成根回合
     * @param year
     * @param month
     * @param step
     */
    public void Start(int year,int month,int step){
        this.month = month;this.year =year;this.step = step;
        GameTurn root = new GameTurn(0,year,month);
        turns.add(root);
    }
    /**
     * 添加国家
     */
    public void addState(State state){
        this.states.add(state);
    }
    /**
     * 进行下个回合
     */
    public void nextTurn(){
        List<State>activeSate =states.stream().filter(o->o.isActive()).collect(Collectors.toList());
        for (State state : activeSate) {
            List<City>activeCity =state.getCityList().stream().filter(o->o.isActive()).collect(Collectors.toList());
            for (City city : activeCity) {
                //对每个城市先进行最基本的数值变化
                //先算出生
                BigDecimal addedPopulation = city.getBirthRate().multiply(BigDecimal.valueOf(city.getPopulation()));
                city.addPopulation( addedPopulation.setScale(0, RoundingMode.DOWN));
                //再算死亡
                //再算经济数值
                BigDecimal addedAgricultureProduction = BigDecimal.valueOf(city.getAgriculturePopulation()).multiply(city.getAgricultureSkillIndex()).multiply(city.getAgricultureUtilIndex()).multiply(city.getAgricultureWillIndex());
                BigDecimal addedCommerceProduction = BigDecimal.valueOf(city.getCommercePopulation()).multiply(city.getCommerceSkillIndex()).multiply(city.getCommerceWillIndex()).multiply(city.getCommerceWillIndex());
                BigDecimal addedIndustryProduction = BigDecimal.valueOf(city.getIndustryPopulation()).multiply(city.getIndustrySkillIndex()).multiply(city.getIndustryUtilIndex()).multiply(city.getIndustryWillIndex());
                BigDecimal addedServiceProduction = BigDecimal.valueOf(city.getServicePopulation()).multiply(city.getServiceSkillIndex()).multiply(city.getServiceUtilIndex()).multiply(city.getServiceWillIndex());
                city.addMoney( addedAgricultureProduction.setScale(6, RoundingMode.HALF_DOWN),addedCommerceProduction.setScale(6, RoundingMode.HALF_DOWN),addedIndustryProduction.setScale(6, RoundingMode.HALF_DOWN),addedServiceProduction);
            }
        }
    }
    /**
     * 单例模式
     * @return
     */
    public static GameManager init(){
        if(gameManager==null){
            gameManager = new GameManager();
        }
        return gameManager;
    }
    private GameManager(){}
}
